package com.goose.bmm.controler.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.WindowManager;

import com.goose.bmm.R;
import com.goose.bmm.controler.fragment.SettingAwayTeamFragment;
import com.goose.bmm.controler.fragment.SettingHomeTeamFragment;
import com.goose.bmm.controler.fragment.SettingMatchFragment;

public class SettingTotalActivity extends ActionBarActivity {

	private ActionBar mActionBar;
	private Tab mTab1, mTab2, mTab3;
	private SettingMatchFragment fragmentSettingMatch;
	private SettingHomeTeamFragment fragmentSettingHomeTeam;
	private SettingAwayTeamFragment fragmentSettingAwayTeam;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_total);

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		mActionBar = getSupportActionBar();
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		mActionBar.setDisplayShowTitleEnabled(false);
		mActionBar.setDisplayUseLogoEnabled(false);
		mTab1 = mActionBar.newTab().setText("Match Setting");
		mTab2 = mActionBar.newTab().setText("Home Team");
		mTab3 = mActionBar.newTab().setText("Away Team");

		fragmentSettingMatch = new SettingMatchFragment();
		fragmentSettingHomeTeam = new SettingHomeTeamFragment();
		fragmentSettingAwayTeam = new SettingAwayTeamFragment();

		mTab1.setTabListener(new MyTabListener(fragmentSettingMatch));
		mTab2.setTabListener(new MyTabListener(fragmentSettingHomeTeam));
		mTab3.setTabListener(new MyTabListener(fragmentSettingAwayTeam));

		mActionBar.addTab(mTab1);
		mActionBar.addTab(mTab2);
		mActionBar.addTab(mTab3);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu items for use in the action bar
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_setting_action_bar, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle presses on the action bar items
		switch (item.getItemId()) {
		case R.id.btn_start:
			// if (testInput()) {
			// Fill data
			fragmentSettingMatch.fillData();
			
			// Start new activity
			Intent intentStartMatchManager = new Intent(
					SettingTotalActivity.this, MatchManagerActivity.class);
			startActivity(intentStartMatchManager);
			finish();
			// }
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onBackPressed() {
		new AlertDialog.Builder(this)
				.setMessage(
						getResources().getString(R.string.dialog_message_back))
				.setPositiveButton(getResources().getString(R.string.yes),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Intent intentStartMainMenu = new Intent(
										SettingTotalActivity.this,
										MainMenuActivity.class);
								startActivity(intentStartMainMenu);
								finish();
							}

						})
				.setNegativeButton(getResources().getString(R.string.no), null)
				.show();
	}

	private boolean testInput() {
		// test home
		try {

			if (((SettingHomeTeamFragment) fragmentSettingHomeTeam)
					.getmEditTextTeamName().getText().toString().isEmpty()) {
				new AlertDialog.Builder(this)
						.setMessage(
								getResources()
										.getString(
												R.string.dialog_massage_enter_home_team_name))
						.setPositiveButton(
								getResources().getString(R.string.ok), null)
						.show();
				return false;
			}
			if (((SettingHomeTeamFragment) fragmentSettingHomeTeam)
					.getmListViewPlayers().getAdapter().getCount() < 5) {
				new AlertDialog.Builder(this)
						.setMessage(
								getResources()
										.getString(
												R.string.dialog_massage_nb_player_home_team_than_more_5))
						.setPositiveButton(
								getResources().getString(R.string.ok), null)
						.show();
				return false;
			}

			if (((SettingHomeTeamFragment) fragmentSettingHomeTeam)
					.getmListPlayerAdapter().getCounterFisrtTeam() < 5) {
				new AlertDialog.Builder(this)
						.setMessage(
								getResources()
										.getString(
												R.string.dialog_massage_enough_5_player_home_team))
						.setPositiveButton(
								getResources().getString(R.string.ok), null)
						.show();
				return false;
			}
		} catch (Exception e) {
			new AlertDialog.Builder(this)
					.setMessage(
							getResources()
									.getString(
											R.string.dialog_message_setup_for_home_team))
					.setPositiveButton(getResources().getString(R.string.ok),
							null).show();
			return false;
		}
		// test away
		try {

			if (((SettingAwayTeamFragment) fragmentSettingAwayTeam)
					.getmEditTextTeamName().getText().toString().isEmpty()) {
				new AlertDialog.Builder(this)
						.setMessage(
								getResources()
										.getString(
												R.string.dialog_massage_enter_away_team_name))
						.setPositiveButton(
								getResources().getString(R.string.ok), null)
						.show();
				return false;
			}
			if (((SettingAwayTeamFragment) fragmentSettingAwayTeam)
					.getmListViewPlayers().getAdapter().getCount() < 5) {
				new AlertDialog.Builder(this)
						.setMessage(
								getResources()
										.getString(
												R.string.dialog_massage_nb_player_away_team_than_more_5))
						.setPositiveButton(
								getResources().getString(R.string.ok), null)
						.show();
				return false;
			}

			if (((SettingAwayTeamFragment) fragmentSettingAwayTeam)
					.getmListPlayerAdapter().getCounterFisrtTeam() < 5) {
				new AlertDialog.Builder(this)
						.setMessage(
								getResources()
										.getString(
												R.string.dialog_massage_enough_5_player_away_team))
						.setPositiveButton(
								getResources().getString(R.string.ok), null)
						.show();
				return false;
			}
		} catch (Exception e) {
			new AlertDialog.Builder(this)
					.setMessage(
							getResources()
									.getString(
											R.string.dialog_message_setup_for_away_team))
					.setPositiveButton(getResources().getString(R.string.ok),
							null).show();
			return false;
		}
		return true;
	}

}
